<?php
header('content-type:text/html;charset=uft-8');
class CacheHtml{
	public $stillPath;
	public $www;
    public $urlfix;
	function __construct($path, $www, $urlfix='.html') {
		if(!is_string($path) || !$path){
			return '静态文件路径不能为空';
		}
		$this->stillPath = $path;
        $this->www = $www;
        $this->urlfix = $urlfix;
	}

    /**
     * Notes: 指定链接生成静态
     * User: ZhuHaili
     * Date: 2020/10/14
     * @param $url
     */
    public function urlfilecreate($url, $urlParamStr = ''){
        $urlFix = substr($url,-strlen($this->urlfix));
        if($urlFix == $this->urlfix){
            $url = substr($url, 0, -strlen($this->urlfix));
        }

        $urlStr = substr($url,0,strlen($this->www));
        if($urlStr == $this->www){
            $url = substr($url, strlen($this->www));
        }
        $this->create($url, $urlParamStr);
    }

    /**
     * Notes:创建静态文件
     * User: ZhuHaili
     */
	public function create($file, $urlParamStr = ''){
        header('content-type:text/html;charset=uft-8');
        $fileArr = $this->getPathFile($file);
        $filePath = $fileArr['filePath'];
        $fileName = $fileArr['fileName'];
		ob_start();								// 启用缓存
        $url = $this->www.$filePath.$fileName.$urlParamStr;
        $cacheFile = file_get_contents($url);
        $file = $fileName.$this->urlfix; 				// 生成文件名
        $fielPathAll = $this->stillPath.'/'.$filePath.$file;
        $fielPathAll = str_replace(array('///','//'),'/',$fielPathAll);
		$fs = fopen($fielPathAll,'w');	// 打开静态页文件
		fwrite($fs,$cacheFile);			        // 生成静态文件
		fclose($fs);							// 关闭指针
        ob_end_flush();
        ob_implicit_flush(1); //立即输出
        //$this->paceInfo($fielPathAll);
	}

    /**
     * Notes:获取文件内容
     * User: ZhuHaili
     */
	public function getStillFile($file){
		$fileArr = $this->getPathFile($file);
		$filePath = $fileArr['filePath'];
		$fileName = $fileArr['fileName'];
		$fileUrl = $this->stillPath.'/'.$filePath.$fileName.$this->urlfix;
        $fileUrl = str_replace(array('///','//'),'/',$fileUrl);
		if(file_exists($fileUrl)){
            echo file_get_contents($fileUrl);
            exit;
		}
	}

    /**
     * Notes:获取文件全路径
     * User: ZhuHaili
     */
	private function getPathFile($file){
		if(!is_string($file) || !$file){
			return array('filePath'=>'','fileName'=>'index');
		}
        $fileArr = explode('/',$file); 		// 拆分路径
		$fileArrNew = array_filter($fileArr);	        // 去除数组空值
		$fileName = array_pop($fileArrNew);		// 删除最后一个元素并获取最后一个元素
		if(strpos($fileName,$this->urlfix) !== false){
			$fileName = substr($fileName, 0, -5);
		}
		$filePath = implode('/',$fileArrNew);	    // 文件路径
        if($filePath){
            $filePath = $filePath.'/';	                // 文件路径
        }
        $filePath = str_replace(array('///','//'),'/',$filePath);
        $createPath = $this->stillPath.$filePath;
		$this->createPath($createPath);			        // 检查路径，没有则创建
		return array('filePath'=>$filePath,'fileName'=>$fileName);
	}

    /**
     * Notes:创建目录
     * User: ZhuHaili
     */
	private function createPath($path){
		if (!file_exists($path)){
			$this->createPath(dirname($path));
			mkdir($path, 0777);
		}
	}

    /**
     * Notes: 进度提示
     * User: ZhuHaili
     */
    public function paceInfo($str){
        //header('content-type:text/html;charset=uft-8');
        @ob_clean();
        ob_start();
        $buffer = str_repeat(" ", 1024)."\r\n<span></span>\r\n";
        // 输出进度信息
        //header('Content-Type: text/event-stream'); // 以事件流的形式告知浏览器进行显示
        //header('Cache-Control: no-cache');         // 告知浏览器不进行缓存
        //header('X-Accel-Buffering: no');           // 关闭加速缓冲
        echo $buffer;
        /*echo '<!DOCTYPE html>
				<html>
				  <head>
					<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
				  </head>
				<body style="font-size:12px;">生成成功：<span style="color:#999;">'.$str.'</span><br></body></html>';*/
        echo $str;
        ob_flush();
        flush();
        ob_end_flush();
    }
}